<template>
  <div class="create">
    <h1 style="text-align:center;font-size:20px;font-weight:bold">选择你要出售的宝贝</h1>
    <el-form
      :rules="rules"
      :model="model"
      label-width="100px"
      @submit.native.prevent="save"
    >
      <el-form-item label="上传图片">
        <el-upload
          class="avatar-uploader"
          :action="uploadUrl"
          :headers="getAuthHeader()"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
        >
          <img
            v-if="model.icon"
            :src="model.icon"
            class="avatar"
          >
          <i
            v-else
            class="el-icon-plus avatar-uploader-icon"
          ></i>
        </el-upload>
      </el-form-item>
      <el-form-item
        label="名称"
        prop="name"
      >
        <el-input v-model="model.name"></el-input>
      </el-form-item>
      <el-form-item
        label="价格"
        prop="price"
      >
        <el-input v-model="model.price"></el-input>
      </el-form-item>
      <el-form-item
        label="出售地"
        prop="place"
      >
        <el-input v-model="model.place"></el-input>
      </el-form-item>
      <el-form-item
        label="备注"
        prop="pc"
      >
        <el-input v-model="model.pc"></el-input>
      </el-form-item>
      <el-form-item
        label="分类"
        prop="col"
      >
        <el-select
          placeholder="请选择"
          v-model="model.col"
        >
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="联系方式"
        prop="phone"
      >
        <el-input v-model="model.phone"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          native-type="submit"
        >提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
var moment = require('moment')
export default {
  props: {
    id: {}
  },
  data: () => ({
    model: {
      icon: ''
    },
    options: [{
      value: 'beauty',
      label: '美妆'
    }, {
      value: 'books',
      label: '书籍'
    }, {
      value: 'digital',
      label: '数码'
    }, {
      value: 'life',
      label: '生活'
    }, {
      value: 'shoesandclothes',
      label: '服饰'
    }, {
      value: 'others',
      label: '其他'
    }],
    rules: {
      name: [
        { required: true, message: '请输入商品名称', trigger: 'blur' }
      ],
      price: [
        { required: true, message: '请填写商品出售价格', trigger: 'change' }
      ],
      place: [
        { required: true, message: '请出售地点', trigger: 'change' }
      ],
      pc: [
        { required: true, message: '请填写备注信息', trigger: 'change' }
      ],
      col: [
        { required: true, message: '请选择商品分类', trigger: 'change' }
      ],
      phone: [
        { required: true, message: '请填写联系电话', trigger: 'change' }
      ]
    }
  }),
  created() {
    // this.fetchParents()
    this.id && this.fetch()
  },
  directives: {
    focus: {
      inserted: function (el) {
        el.children[0].focus()
      }
    }
  },
  methods: {
    async save() {
      this.model.data = moment().format('YYYY-MM-DD hh:mm:ss')
      this.model.seller = this.$store.state.user[0].username
      console.log(this.model)
      // this.$router.push('/grids/list')
      await this.$http.upbaby(this.model)
      // let res
      // if (this.id) {
      //   res = await this.$http.put('/admin/api/rest/grids/' + this.id, this.model)
      // } else {
      //   res = await this.$http.post('/admin/api/rest/grids', this.model)
      // }
      // console.log(res)
      this.$message({
        type: 'success',
        message: '上传成功'
      })
      this.$router.push('/list')
    },
    async fetch() {
      const res = await this.$http.get('/admin/api/rest/grids/' + this.id)
      this.model = res.data
    },
    handleAvatarSuccess(res) {
      // console.log(res)
      this.model.icon = res.url
      // 显示赋值
      // this.$set(this.model, 'icon', res.url)
    }
  }
}
</script>
<style lang="scss" >
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.el-icon-plus:before {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
</style>
